package com.lvkerry.orderfood.controller

import com.lvkerry.orderfood.api.ApiBean
import com.lvkerry.orderfood.global.API_ERROR
import com.lvkerry.orderfood.global.API_SUCCESS
import com.lvkerry.orderfood.service.MenuService
import com.lvkerry.orderfood.service.OrderService
import com.lvkerry.orderfood.service.UserService
import com.lvkerry.orderfood.service.ResourceService
import com.lvkerry.orderfood.util.convertStringToTimeStamp
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.multipart.MultipartFile


@Controller
@RequestMapping("/api")
class ApiController(private val menuService: MenuService, private val orderService: OrderService,
                    private val userService: UserService, private val resourceService: ResourceService) {


    @ResponseBody
    @PostMapping("/findAllMenu")
    fun findAllMenu(): ApiBean {
        return menuService.findAllMenu()
    }


    @ResponseBody
    @PostMapping("/checkToken")
    fun checkToken(): ApiBean {
        return ApiBean.Builder().setStatus(API_SUCCESS).build()
    }


    @ResponseBody
    @PostMapping("/getMenuDetail")
    fun getMenuDetail(menuId: Long): ApiBean {
        return menuService.findMenuDetail(menuId)
    }

    @ResponseBody
    @PostMapping("/checkUserTodayIsOrder")
    fun checkUserTodayIsOrder(): ApiBean {
        return orderService.checkUserTodayOrder()
    }

    @ResponseBody
    @PostMapping("/order")
    fun order(menuId: Long, orderStatus: Int = 0, remark: String): ApiBean {
        return try {
            orderService.order(menuId, orderStatus, remark)
        } catch (e: Exception) {
            ApiBean.Builder().setStatus(API_ERROR).setMessage(e.message).build()
        }
    }

    @ResponseBody
    @PostMapping("/updateOrder")
    fun updateOrder(orderNo: String, menuId: Long, remark: String?): ApiBean {
        return try {
            orderService.updateOrder(orderNo, menuId, remark)
        } catch (e: Exception) {
            ApiBean.Builder().setStatus(API_ERROR).setMessage(e.message).build()
        }
    }


    @ResponseBody
    @PostMapping("/cancelUserOrder")
    fun cancelUserOrder(): ApiBean {
        return try {
            orderService.cancelUserOrder()
        } catch (e: Exception) {
            ApiBean.Builder().setStatus(API_ERROR).setMessage(e.message).build()
        }
    }

    @ResponseBody
    @PostMapping("/getUserInfo")
    fun getUserInfo(): ApiBean {
        return try {
            return userService.userInfo
        } catch (e: Exception) {
            ApiBean.Builder().setStatus(API_ERROR).setMessage(e.message).build()
        }
    }


    @ResponseBody
    @PostMapping("/getOneDayAllOrders")
    fun getAllTodayOrder(lunchDate: String): ApiBean {
        return orderService.getOneDayAllOrders(lunchDate.trim())
    }

    @ResponseBody
    @PostMapping("/getHistoryOrder")
    fun getHistoryOrder(startDate: String, page: Int = 0, pageCount: Int = 40): ApiBean {
        val timeStamp = convertStringToTimeStamp(startDate)

        return orderService.getUserHistoryOrder(timeStamp, page, pageCount)
    }


    @ResponseBody
    @PostMapping("/uploadImage")
    fun uploadUserAvatar(@RequestParam("file") file: MultipartFile?): ApiBean {

        when {
            file == null -> ApiBean.Builder().setMessage("file is null").setStatus(API_ERROR).build()
            file.isEmpty -> ApiBean.Builder().setMessage("failed to upload").setStatus(API_ERROR).build()
            else -> return try {
                resourceService.saveResource(file)
            } catch (e: Exception) {
                ApiBean.Builder().setMessage("failed to upload ${e.message}").setStatus(API_ERROR).build()
            }
        }
        return ApiBean.Builder().setMessage("unknow error").setStatus(API_ERROR).build()
    }

    @ResponseBody
    @PostMapping("/updatePassword")
    fun updatePassword(newPw1: String, newPw2: String): ApiBean {

        return userService.updatePassword(newPw1, newPw2)
    }


}
